{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 排序搜索计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**如何通过第n列对一个数组进行排序**\n",
    "\n",
    "【知识点:排序】\n",
    "\n",
    "- (提示: argsort)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:34.985841Z",
     "start_time": "2020-09-15T07:06:34.567440Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 6 0]\n",
      " [7 0 3]\n",
      " [0 6 0]]\n"
     ]
    }
   ],
   "source": [
    "Z = np.random.randint(0,10,(3,3))\n",
    "print (Z)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:35.022771Z",
     "start_time": "2020-09-15T07:06:35.000801Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 6 0]\n",
      " [0 6 0]\n",
      " [7 0 3]]\n"
     ]
    }
   ],
   "source": [
    "print (Z[Z[:,2].argsort()])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-11T12:42:05.895859Z",
     "start_time": "2020-10-11T12:42:05.891836Z"
    }
   },
   "source": [
    "**对x的列进行排序，先按照最后一行排序，假如两数相等则按照倒数第二行排序**\n",
    "\n",
    "【知识点:排序】\n",
    "\n",
    "- (提示: lexsort)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-11T12:43:13.585247Z",
     "start_time": "2020-10-11T12:43:13.582255Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "x=np.array([[1,2,3,4],[1,4,3,3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-11T12:43:21.756991Z",
     "start_time": "2020-10-11T12:43:21.750043Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 3, 1], dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 4, 2],\n",
       "       [1, 3, 3, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y=np.lexsort(x,axis=0)\n",
    "display(y)\n",
    "display(x[:,y])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**取出每一列比第三大的数字小的数**\n",
    "【知识点：排序】\n",
    "- （提示：np.argpartition/np.partition）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-11T13:42:41.515755Z",
     "start_time": "2020-10-11T13:42:41.511766Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 9 25  4]\n",
      " [ 8 24 16]\n",
      " [17 11 21]\n",
      " [ 3 22  3]\n",
      " [ 3 15  3]\n",
      " [18 17 25]\n",
      " [16  5 12]\n",
      " [29 27 17]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(100)\n",
    "x = np.random.randint(1, 30, [8, 3])\n",
    "print(x)\n",
    "# [[ 9 25  4]\n",
    "#  [ 8 24 16]\n",
    "#  [17 11 21]\n",
    "#  [ 3 22  3]\n",
    "#  [ 3 15  3]\n",
    "#  [18 17 25]\n",
    "#  [16  5 12]\n",
    "#  [29 27 17]]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-11T13:42:42.122927Z",
     "start_time": "2020-10-11T13:42:42.117940Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3  5  3]\n",
      " [ 3 11  3]]\n"
     ]
    }
   ],
   "source": [
    "z = np.argpartition(x, kth=2, axis=0)\n",
    "y=np.array([[ x[z[i,j],j]for j in range(3)]for i in range(2)])\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-11T13:19:25.176370Z",
     "start_time": "2020-10-11T13:19:25.172381Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  5,  3],\n",
       "       [ 3, 11,  3]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.partition(x, kth=2, axis=0)[:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T02:43:15.537614Z",
     "start_time": "2020-09-06T02:43:15.532628Z"
    }
   },
   "source": [
    "**从`arr`中提取所有奇数。**\n",
    "\n",
    "- `arr = np.arange(10)`\n",
    "\n",
    "【知识点：搜索】\n",
    "- 如何从一维数组中提取满足指定条件的元素？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:35.384825Z",
     "start_time": "2020-09-15T07:06:35.376847Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 5 7 9]\n",
      "[1 3 5 7 9]\n"
     ]
    }
   ],
   "source": [
    "#【答案】\n",
    "import numpy as np\n",
    "\n",
    "arr = np.arange(10)\n",
    "\n",
    "# 方法1\n",
    "index = np.where(arr % 2 == 1)\n",
    "print(arr[index])\n",
    "# [1 3 5 7 9]\n",
    "\n",
    "# 方法2\n",
    "x = arr[arr % 2 == 1]\n",
    "print(x)\n",
    "# [1 3 5 7 9]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**将`arr`中的偶数元素替换为0。**\n",
    "\n",
    "- `arr = np.arange(10)`\n",
    "\n",
    "【知识点：搜索】\n",
    "- 如何用numpy数组中的另一个值替换满足条件的元素项？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:35.907995Z",
     "start_time": "2020-09-15T07:06:35.903011Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 0 3 0 5 0 7 0 9]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.arange(10)\n",
    "index = np.where(arr % 2 == 0)\n",
    "arr[index] = 0\n",
    "print(arr)\n",
    "# [0 1 0 3 0 5 0 7 0 9]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**将 `arr` 中的所有偶数元素替换为0，而不改变arr。**\n",
    "- `arr = np.arange(10)`\n",
    "\n",
    "【知识点：搜索】\n",
    "- 如何在不影响原始数组的情况下替换满足条件的元素项？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:36.689514Z",
     "start_time": "2020-09-15T07:06:36.608498Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 0 3 0 5 0 7 0 9]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[0 1 0 3 0 5 0 7 0 9]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.arange(10)\n",
    "\n",
    "# 方法1\n",
    "x = np.where(arr % 2 == 0, 0, arr)\n",
    "print(x)\n",
    "# [0 1 0 3 0 5 0 7 0 9]\n",
    "print(arr)\n",
    "# [0 1 2 3 4 5 6 7 8 9]\n",
    "\n",
    "# 方法2\n",
    "x = np.copy(arr)\n",
    "x[x % 2 == 0] = 0\n",
    "print(x)\n",
    "# [0 1 0 3 0 5 0 7 0 9]\n",
    "print(arr)\n",
    "# [0 1 2 3 4 5 6 7 8 9]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:10:15.790588Z",
     "start_time": "2020-09-06T03:10:15.785591Z"
    }
   },
   "source": [
    "**获取给定数组a中前5个最大值的位置。**\n",
    "\n",
    "- `a = np.random.uniform(1, 50, 20)`\n",
    "\n",
    "【知识点：搜索】\n",
    "- 如何从numpy数组中获取最大的n个值的位置？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:37.223217Z",
     "start_time": "2020-09-15T07:06:37.205278Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[27.62684215 14.64009987 21.80136195 42.39403048  1.23122395  6.95688692\n",
      " 33.86670515 41.466785    7.69862289 29.17957314 44.67477576 11.25090398\n",
      " 10.08108276  6.31046763 11.76517714 48.95256545 40.77247431  9.42510962\n",
      " 40.99501269 14.42961361]\n",
      "[ 4 13  5  8 17 12 11 14 19  1  2  0  9  6 16 18  7  3 10 15]\n",
      "[18  7  3 10 15]\n",
      "(array([ 3,  7, 10, 15, 18], dtype=int64),)\n",
      "[18  7  3 10 15]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(100)\n",
    "a = np.random.uniform(1, 50, 20)\n",
    "print(a)\n",
    "# [27.62684215 14.64009987 21.80136195 42.39403048  1.23122395  6.95688692\n",
    "#  33.86670515 41.466785    7.69862289 29.17957314 44.67477576 11.25090398\n",
    "#  10.08108276  6.31046763 11.76517714 48.95256545 40.77247431  9.42510962\n",
    "#  40.99501269 14.42961361]\n",
    "\n",
    "# 方法1\n",
    "b = np.argsort(a)\n",
    "print(b)\n",
    "print(b[-5:])\n",
    "# [18  7  3 10 15]\n",
    "\n",
    "# 方法2\n",
    "b = np.sort(a)\n",
    "b = np.where(a >= b[-5])\n",
    "print(b)\n",
    "# (array([ 3,  7, 10, 15, 18], dtype=int64),)\n",
    "\n",
    "# 方法3\n",
    "b = np.argpartition(a, kth=-5)\n",
    "print(b[-5:])\n",
    "# [18  7  3 10 15]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:16:45.278429Z",
     "start_time": "2020-09-06T03:16:45.274474Z"
    }
   },
   "source": [
    "**删除一维numpy数组中所有NaN值。**\n",
    "\n",
    "- `a = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan])`\n",
    "\n",
    "【知识点：逻辑函数、搜索】\n",
    "- 如何删除numpy数组中的缺失值？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:37.705909Z",
     "start_time": "2020-09-15T07:06:37.699887Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 5. 6. 7.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3, np.nan, 5, 6, 7, np.nan])\n",
    "b = np.isnan(a)\n",
    "c = np.where(np.logical_not(b))\n",
    "print(a[c])\n",
    "# [1. 2. 3. 5. 6. 7.]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**获取给定数组a中比7大的数有多少。**\n",
    "\n",
    "- `a = np.random.uniform(1, 50, 20)`\n",
    "\n",
    "【知识点：搜索】\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-11T14:06:10.903242Z",
     "start_time": "2020-10-11T14:06:10.897219Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[35.37289863 19.89303398  1.85370469  3.49737665 22.31851519 48.86534949\n",
      "  5.59538215 28.0244556  32.512054   22.572477  ]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.random.uniform(1, 50, 10)\n",
    "print(a)\n",
    "len(a)-np.searchsorted(a,7,side='right')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T02:49:53.817130Z",
     "start_time": "2020-09-06T02:49:53.812188Z"
    }
   },
   "source": [
    "**获取数组a和数组b之间的公共项。**\n",
    "\n",
    "- `a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])`\n",
    "- `b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])`\n",
    "\n",
    "【知识点：集合操作】\n",
    "- 如何获取两个numpy数组之间的公共项？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:38.496578Z",
     "start_time": "2020-09-15T07:06:38.489570Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])\n",
    "b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])\n",
    "x = np.intersect1d(a, b)\n",
    "print(x)  # [2 4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T02:50:45.262179Z",
     "start_time": "2020-09-06T02:50:45.257192Z"
    }
   },
   "source": [
    "**从数组a中删除数组b中的所有项。**\n",
    "- `a = np.array([1, 2, 3, 4, 5])`\n",
    "- `b = np.array([5, 6, 7, 8, 9])`\n",
    "\n",
    "【知识点：集合操作】\n",
    "- 如何从一个数组中删除存在于另一个数组中的项？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-15T07:06:39.018110Z",
     "start_time": "2020-09-15T07:06:38.996083Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([5, 6, 7, 8, 9])\n",
    "x = np.setdiff1d(a, b)\n",
    "print(x)  # [1 2 3 4]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python35",
   "language": "python",
   "name": "python35"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307.2px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
